package cn.sylinx.horm.example.section9;

import java.util.List;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import cn.sylinx.horm.core.SqlClient;
import cn.sylinx.horm.dialect.fs.FS;
import cn.sylinx.horm.example.entity.MyExample6;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Profile("section9")
@Component
public class Section9 implements CommandLineRunner {

    private static final String FIRST = "primaryDataSource";
    private static final String SECOND = "secondaryDataSource";

    private SqlClient sqlClient;

    public Section9(SqlClient sqlClient) {
        this.sqlClient = sqlClient;
    }

    @Override
    public void run(String... args) throws Exception {
        test();
    }

    void test() {

        // 默认数据源
        List<MyExample6> dataList = sqlClient.query(FS.of(MyExample6.class).limit(1));
        log.info("默认数据源 -> {}, dataList -> {}", sqlClient.getDataSourceName(), dataList);

        // 第一个数据源 , primaryDataSource 也是默认数据源
        SqlClient firstSqlClient = SqlClient.getClientBy(FIRST);
        List<MyExample6> dataList1 = firstSqlClient.query(FS.of(MyExample6.class).limit(1));
        log.info("第一个数据源 -> {}, dataList1 -> {}", firstSqlClient.getDataSourceName(), dataList1);

        // 第二个数据源
        SqlClient secondSqlClient = SqlClient.getClientBy(SECOND);
        List<MyExample6> dataList2 = secondSqlClient.query(FS.of(MyExample6.class).limit(1));
        log.info("第二个数据源 -> {}, dataList2 -> {}", secondSqlClient.getDataSourceName(), dataList2);
    }
}
